package com.zhangtai.modules.service.impl.promotion;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangtai.modules.dao.PromotionDao;
import com.zhangtai.modules.dto.PromotionEntityDto;
import com.zhangtai.modules.entity.employee.PromotionEntity;
import com.zhangtai.modules.service.promotion.PromotionService;
import com.zhangtai.utils.PageResult;
import com.zhangtai.utils.R;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;


@RestController
public class PromotionServiceImpl extends ServiceImpl<PromotionDao, PromotionEntity> implements PromotionService {

    @Autowired
    private PromotionDao promotionDao;

    @Override
    public PageResult queryPage(@RequestParam(value = "keyword", required = false) String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {

        IPage<PromotionEntity> page = this.page(
                new Page<PromotionEntity>(pageNum,pageSize),
                new QueryWrapper<PromotionEntity>().like(!StringUtils.isEmpty(keyword),"name",keyword)
                        .eq("is_delete",0)
        );

        return new PageResult(page);
    }

    @Override
    public PageResult<PromotionEntityDto> queryPage(@Param("promotionType")Integer promotionType, @RequestParam("userCode")String userCode,
                                                    @RequestParam("name")String name,  @RequestParam("orgName")String orgName,
                                                    @RequestParam("departmentName")String departmentName, @RequestParam("expeditionStatus")Integer expeditionStatus,
                                                    @RequestParam("isDistribute")Integer isDistribute,@RequestParam("orgIds")List<Long> orgIds,
                                                    @RequestParam("promotionKind")Integer promotionKind, @RequestParam(value ="isSelf")Integer isSelf,
                                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                 @RequestParam(value = "status",required = false)Integer status
    ) {
        Page<PromotionEntityDto> page = new Page<PromotionEntityDto>(pageNum,pageSize);
        List<PromotionEntityDto> list = promotionDao.getPromotionList(promotionType,userCode,name,
                orgName,departmentName,expeditionStatus,isDistribute,orgIds,promotionKind,isSelf,status,page);
        page.setRecords(list);
        return new PageResult<PromotionEntityDto>(page);
    }


    @Override
    public PromotionEntity getByIdE(Long id) {
            PromotionEntity one = this.getOne(new QueryWrapper<PromotionEntity>().eq("promotion_id", id).eq("is_delete", 0));

        return one;
    }


    @Override
    public R saveE(PromotionEntity entity) {
        boolean b = this.save(entity);
        if (!b){
            return R.error();
        }
        return R.ok(entity);

    }

    @Override
    public R updateByIdE(PromotionEntity entity) {
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    public R removeOneById(Long id) {
            PromotionEntity entity = new PromotionEntity();
        entity.setPromotionId(id);
        entity.setIsDelete(1);
        boolean b = this.updateById(entity);
        if (!b){
            return R.error();
        }
        return R.ok();
    }

    @Override
    @Transactional
    public R removeByIdsE(List<Long> ids) {
        for (Long id : ids) {
            R r = removeOneById(id);
            if (!r.getFlag()){
                return r;
            }
        }
        return R.ok();
    }



}